//
//  ItemDataBase.m
//  BeautyClother
//
//  Created by dllo on 15/9/2.
//  Copyright (c) 2015年 宋爽. All rights reserved.
//

#import "ItemDataBase.h"
#import "ItemsModel.h"
#import "DataBase.h"

@implementation ItemDataBase


+ (void)insertWithModel:(ItemsModel *)model
{
    // 第一步: 获得数据库指针
    sqlite3 *db = [DataBase dataBaseOpen];

    // 第二步: 获得数据库语句

    NSString *str = [NSString stringWithFormat:@"insert into items(iid,description,collectionCount,itemsCount,picUrl,height,width) values('%@','%@','%@','%@','%@', '%f', '%f')", model.iid, model.desc, model.collectionCount, model.itemsCount, model.picUrl, model.height, model.width];
    NSLog(@"%@", str);

    // 第三步: 执行sql语句
    int result = sqlite3_exec(db, [str UTF8String], nil, nil, nil);

    // 判断执行是否成功
    if (result == SQLITE_OK) {
        NSLog(@"执行成功");
    }else
    {
        NSLog(@"执行失败");
    }

}


+ (void)updateWithModel:(ItemsModel *)model
{
    // 第一步: 获得数据库指针
    sqlite3 *db = [DataBase dataBaseOpen];

    // 第二步: 获得数据库语句

    NSString *str = [NSString stringWithFormat:@"update items set like='%@' where iid='%@'",  model.collectionCount, model.iid];

    // 第三步: 执行sql语句
    int result = sqlite3_exec(db, [str UTF8String], nil, nil, nil);

    // 判断执行是否成功
    if (result == SQLITE_OK) {
        NSLog(@"更新成功");
    }else
    {
        NSLog(@"更新失败");
    }

}


+ (void)deleteWithid:(NSString *)iid
{
    // 第一步: 获得数据库指针
    sqlite3 *db = [DataBase dataBaseOpen];
    
    // 第二步: 获得数据库语句
    
    NSString *str = [NSString stringWithFormat:@"delete from items where iid='%@'", iid];
    
    // 第三步: 执行sql语句
    int result = sqlite3_exec(db, [str UTF8String], nil, nil, nil);
    
    // 判断执行是否成功
    if (result == SQLITE_OK) {
        NSLog(@"删除1成功");
    }else
    {
        NSLog(@"删除失败");
    }
    
}

+ (void)deleteWithModel:(ItemsModel *)model
{
    // 第一步: 获得数据库指针
    sqlite3 *db = [DataBase dataBaseOpen];

    // 第二步: 获得数据库语句

    NSString *str = [NSString stringWithFormat:@"delete from items where iid='%@'", model.iid];

    // 第三步: 执行sql语句
    int result = sqlite3_exec(db, [str UTF8String], nil, nil, nil);

    // 判断执行是否成功
    if (result == SQLITE_OK) {
        NSLog(@"删除成功");
    }else
    {
        NSLog(@"执行失败");
    }

}

#pragma mark -
#pragma mark 查询
// 查询所有的:
+ (NSArray *)selectAll
{
    /* 1. 获得数据库指针 */
    sqlite3 *db = [DataBase dataBaseOpen];

    /* 2. 定义数据库替身 */
    sqlite3_stmt *stmt = nil;

    /* 数据库语句 */
    NSString *str = [NSString stringWithFormat:@"select * from items"];

    /* 执行 */ // -1 代表语句无限长
    int result = sqlite3_prepare_v2(db, [str UTF8String], -1, &stmt, nil);

    NSMutableArray *array = [NSMutableArray array];




    if (result == SQLITE_OK) {
        // 判断是否有下一行数据可用于读取
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            const unsigned char *_iid = sqlite3_column_text(stmt,0);
            const unsigned char *titlee = sqlite3_column_text(stmt,1);
            const unsigned char *imagee = sqlite3_column_text(stmt,2);
            const unsigned char *likee = sqlite3_column_text(stmt,3);
            const unsigned char *sharee = sqlite3_column_text(stmt,4);
            float width = sqlite3_column_double(stmt, 5);
            float hei = sqlite3_column_double(stmt, 6);
            

            // stringWithUTF8String 如果数据库里的某一行的某一个字段是空的 会crash
            NSString *iid = [NSString stringWithUTF8String:(const char *)_iid];
            NSString *collectionCount = [NSString stringWithUTF8String:(const char *)likee];
            NSString *picUrl = [NSString stringWithUTF8String:(const char *)imagee];
            NSString *itemsCount = [NSString stringWithUTF8String:(const char *)sharee];
            NSString *desc = [NSString stringWithUTF8String:(const char *)titlee];


            ItemsModel *model = [ItemsModel itemsModelWithiid:iid picUrl:picUrl collectionCount:collectionCount itemsCount:itemsCount desc:desc height:hei width:width];
            [array addObject:model];
        }
    }
    sqlite3_finalize(stmt);
    

    return array;

}


+ (ItemsModel *)selectWith:(NSInteger)myId
{
    /* 1. 获得数据库指针 */
    sqlite3 *db = [DataBase dataBaseOpen];

    /* 2. 定义数据库替身 */
    sqlite3_stmt *stmt = nil;

    /* 数据库语句 */
    NSString *str = [NSString stringWithFormat:@"select * from merry where mid='%ld'", myId];

    /* 执行 */ // -1 代表语句无限长
    int result = sqlite3_prepare_v2(db, [str UTF8String], -1, &stmt, nil);



    if (result == SQLITE_OK) {
        // 判断是否有下一行数据可用于读取
        if (sqlite3_step(stmt) == SQLITE_ROW) {
            const unsigned char *_iid = sqlite3_column_text(stmt,0);
            const unsigned char *titlee = sqlite3_column_text(stmt,1);
            const unsigned char *imagee = sqlite3_column_text(stmt,2);
            const unsigned char *likee = sqlite3_column_text(stmt,3);
            const unsigned char *sharee = sqlite3_column_text(stmt,4);
            float hei = sqlite3_column_double(stmt, 5);
            float width = sqlite3_column_double(stmt, 6);

            // stringWithUTF8String 如果数据库里的某一行的某一个字段是空的 会crash
            NSString *iid = [NSString stringWithUTF8String:(const char *)_iid];
            NSString *collectionCount = [NSString stringWithUTF8String:(const char *)likee];
            NSString *picUrl = [NSString stringWithUTF8String:(const char *)imagee];
            NSString *itemsCount = [NSString stringWithUTF8String:(const char *)sharee];
            NSString *desc = [NSString stringWithUTF8String:(const char *)titlee];
            ItemsModel *model = [ItemsModel itemsModelWithiid:iid picUrl:picUrl collectionCount:collectionCount itemsCount:itemsCount desc:desc height:hei width:width];
            sqlite3_finalize(stmt);
            return model;
        }
    }
    sqlite3_finalize(stmt);

    return nil;

}

+ (BOOL)selectWithid:(NSString *)myId
{
    /* 1. 获得数据库指针 */
    sqlite3 *db = [DataBase dataBaseOpen];

    /* 2. 定义数据库替身 */
    sqlite3_stmt *stmt = nil;

    /* 数据库语句 */
    NSString *str = [NSString stringWithFormat:@"select * from items where iid='%@'", myId];

    /* 执行 */ // -1 代表语句无限长
    int result = sqlite3_prepare_v2(db, [str UTF8String], -1, &stmt, nil);



    if (result == SQLITE_OK) {
        // 判断是否有下一行数据可用于读取
        if (sqlite3_step(stmt) == SQLITE_ROW) {
            const unsigned char *_iid = sqlite3_column_text(stmt,0);
            const unsigned char *titlee = sqlite3_column_text(stmt,1);
            const unsigned char *imagee = sqlite3_column_text(stmt,2);
            const unsigned char *likee = sqlite3_column_text(stmt,3);
            const unsigned char *sharee = sqlite3_column_text(stmt,4);
            float hei = sqlite3_column_double(stmt, 5);
            float width = sqlite3_column_double(stmt, 6);


            // stringWithUTF8String 如果数据库里的某一行的某一个字段是空的 会crash
            NSString *iid = [NSString stringWithUTF8String:(const char *)_iid];
            NSString *collectionCount = [NSString stringWithUTF8String:(const char *)likee];
            NSString *picUrl = [NSString stringWithUTF8String:(const char *)imagee];
            NSString *itemsCount = [NSString stringWithUTF8String:(const char *)sharee];
            NSString *desc = [NSString stringWithUTF8String:(const char *)titlee];
            [ItemsModel itemsModelWithiid:iid picUrl:picUrl collectionCount:collectionCount itemsCount:itemsCount desc:desc height:hei width:width];
            sqlite3_finalize(stmt);
            return YES;
        }
    }
    sqlite3_finalize(stmt);

    return NO;

}




@end
